package Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.newsapplication.MainActivity;
import com.example.newsapplication.R;

public class WelcomeActivity extends AppCompatActivity {


    //开启一个新的线程
    final Handler handler = new Handler(){
        public void handleMessage(Message msg){
            super.handleMessage(msg);
//            Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
//            startActivity(intent);
            if(msg.what == 1){
                //判断用户是否登录
                boolean userIsLogin = false;
                if(userIsLogin){
                    Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(WelcomeActivity.this,LoginOrRegisterActivity.class);
                    startActivity(intent);
                }
                finish();
            }else if(msg.what == 0){
                thread.interrupt();
            }
        }
    };

    final Message message = new Message();
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //3秒后跳转
                    Thread.sleep(3000);
                    message.what = 1;
                    handler.sendMessage(message);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        thread.start();
    }
}